package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiCommonService;
import com.bsh.service.ApiMessageService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
import com.bsh.utils.Des3;
import com.bsh.utils.IpUtil;

/**
 * 通用相关
 * 
 * @author Administrator
 *
 */
@Controller
public class ApiCommonController {
	private static Logger log = LoggerFactory.getLogger(ApiUserController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiCommonService apiCommonService;
	@Autowired
	ApiMessageService apiMessageService;


	/**
	 * 首页轮播图
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/getIndexImages", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage getIndexImages() {
		ApiMessage msg = new ApiMessage();
		JSONObject dto = new JSONObject();
		try {
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCommonService.getIndexImages(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}
	/**
	 * 商户轮播图
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/getMerchantIndexImages", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage getMerchantIndexImages() {
		ApiMessage msg = new ApiMessage();
		JSONObject dto = new JSONObject();
		try {
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCommonService.getMerchantIndexImages(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 查询是否是商户
	 * 
	 * @param paramStr
	 * @param headPic
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/isMerchant", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage isMerchant(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCommonService.isMerchant(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}
	
	
	
	/**
	 * 查询实名认证信息
	 * 
	 * @param paramStr
	 * @param headPic
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/getUserVerifyInfo", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getUserVerifyInfo(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiCommonService.getUserVerifyInfo(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

		
	}

	/**
	 * 加密字符并输出
	 * 
	 * @param
	 * @return encode data string
	 */
	@RequestMapping(value = "/api/v1/common/encodes", method = { RequestMethod.POST })
	@ResponseBody
	public String encodesData(@RequestParam String paramStr) {

		String data = Des3.enc(paramStr);
		return data;
	}

	/**
	 * 解密字符并输出
	 * 
	 * @param
	 * @return decode data string
	 */
	@RequestMapping(value = "/api/v1/common/decodes", method = { RequestMethod.POST })
	@ResponseBody
	public String decodesData(@RequestParam String paramStr) {

		String data = Des3.dec(paramStr);
		return data;
	}

	/**
	 * 测试
	 * 

	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/v1/common/test")
	@ResponseBody
	public ApiMessage test() throws Exception {
		return new ApiMessage("200", "测试..."+IpUtil.getIpAddr(request));
	}

	/**
	 * 图片上传
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/uploadPic")
	@ResponseBody
	public ApiMessage uploadPic(@RequestParam(value = "uploadPic", required = true) MultipartFile uploadPic){
		ApiMessage msg = new ApiMessage();
		JSONObject dto = new JSONObject();
		try {
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiCommonService.uploadPic(dto,uploadPic);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
    /**
     * 分页查询
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/queryPage")
	@ResponseBody
	public ApiMessage queryPage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.queryPage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
	}
    /**
     * 更新商户
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/updatePage")
	@ResponseBody
	public ApiMessage updatePage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.updatePage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
    /**
     * 查询用户
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/queryUser")
	@ResponseBody
	public ApiMessage queryUser(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.queryUser(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
		
	}
	
	/**
     * 查询用户分页
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/queryUserPage")
	@ResponseBody
	public ApiMessage queryUserPage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.queryUserPage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
	/**
     * 更新查询分页
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/updateUserPage")
	@ResponseBody
	public ApiMessage updateUserPage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.updateUserPage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
	/**
     * 添加查询分页
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/insertUserPage")
	@ResponseBody
	public ApiMessage insertUserPage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.insertUserPage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
	/**
     * 删除查询分页
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/deleteUserPage")
	@ResponseBody
	public ApiMessage deleteUserPage(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.deleteUserPage(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
	/**
     * 批量更新分页
     * @param paramStr
     * @return
     * @throws Exception
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/updatePages")
	@ResponseBody
	public ApiMessage updatePages(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.updatePages(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}
		
	}
	
	
    /**
     * 添加商户
     * @param paramStr
     * @return
     */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/add", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage addMerchant(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.addMerchant(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
		} finally {
			return msg;
		}

	}
	/**
	 * 意见反馈
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/appendFeedBack", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage apiAuthNameIdentify(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiMessageService.appendFeedBack(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR,Constants.MSG_FAILED);
		} finally {
			return msg;
		}
	}

	
	
	/**
	 * getMerchantDetail
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/common/getMerchantDetail", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getMerchantDetail(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiCommonService.getMerchantDetail(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR,Constants.MSG_FAILED);
		} finally {
			return msg;
		}
	}
	
}
